package com.server.llh.llhserverwork.activity.my;


import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TabHost;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.lelaohui.net.netmanager.SystemStatusManager;
import com.server.llh.llhserverwork.MainActivity;
import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.activity.infomation.InfoMationActivity;
import com.server.llh.llhserverwork.view.AlertDialog;

import butterknife.BindView;
import butterknife.ButterKnife;


@SuppressWarnings("deprecation")
public class MainTabActivity extends TabActivity implements BottomNavigationBar.OnTabSelectedListener {
    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;
    private TabHost mHost;
    private Intent finish, three, four;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setFullScreen(false);
        super.onCreate(savedInstanceState);
        new SystemStatusManager(this).setTranslucentStatus(com.lelaohui.net.R.color.top_color_title);
        setContentView(R.layout.main_tab);
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        initView();
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.llh_nav_1, "首页").setActiveColorResource(R.color.orange1))
                .addItem(new BottomNavigationItem(R.mipmap.llh_nav_3, "资讯").setActiveColorResource(R.color.blue1))
                .addItem(new BottomNavigationItem(R.mipmap.llh_nav_4, "我的").setActiveColorResource(R.color.red))
                .setFirstSelectedPosition(0)
                .initialise();
        bottomNavigationBar.setTabSelectedListener(this);
    }
    public void initView() {
        this.finish = new Intent(this, MainActivity.class);
        this.three = new Intent(this,InfoMationActivity.class);
        this.four=new Intent(this, MyActivity.class);
        setupIntent();
    }

    private void setupIntent() {
        this.mHost = getTabHost();
        TabHost localTabHost = this.mHost;
        localTabHost.addTab(buildTabSpec("finish", "首页", this.finish));
        localTabHost.addTab(buildTabSpec("three", "咨询", this.three));
        localTabHost.addTab(buildTabSpec("four", "我的", this.four));
    }
    private TabHost.TabSpec buildTabSpec(String tag, String resLabel,
                                         final Intent content) {
        return this.mHost.newTabSpec(tag).setIndicator(resLabel)
                .setContent(content);
    }
    @Override
    public void onTabSelected(int position) {
        if (position == 0) {
            this.mHost.setCurrentTabByTag("finish");
        } else if (position == 1) {
            this.mHost.setCurrentTabByTag("three");
        }else{
            this.mHost.setCurrentTabByTag("four");
        }
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    public void setFullScreen(boolean fullScreen) {
        if (fullScreen) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
    }

    private long firstTime = 0;

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getRepeatCount() == 0) {
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) {
                    firstTime = secondTime;
                    return true;
                } else {
                    moveTaskToBack(false);
//                    showInfo();
                }
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }

    private void showInfo() {

        AlertDialog.Builder builder = new AlertDialog.Builder(MainTabActivity.this);
        builder.setTitle("温馨提示");
        builder.setMessage("\n" + "您确定要退出应用么？" + "\n");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                System.exit(0);
                finish();
                dialog.cancel();
            }
        });
        builder.show();
    }
}
